package ru.autofan.action;

import javax.servlet.http.HttpServletResponse;

import org.springframework.webflow.context.servlet.ServletExternalContext;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

import ru.autofan.logic.CarFotoService;
import ru.autofan.view.CarFotoBean;

public class ShowCarPhotoAction extends AbstractAction {

	public Event getCar(RequestContext context) throws Exception {
		Long id = Long.valueOf(context.getExternalContext().getRequestParameterMap().get("id"));
        ServletExternalContext externalContext =  (ServletExternalContext) context.getExternalContext();
        HttpServletResponse response = externalContext.getResponse();
        
        CarFotoBean bean = getService(CarFotoService.class).getCar(id);
        response.setContentType(bean.getContentType());
        response.setCharacterEncoding("UTF-8");
        response.getOutputStream().write(bean.getData());
       
        response.flushBuffer();
        response.getOutputStream().close();

        return success();
	}
	
}
